
let show=document.getElementById("show")
let search=document.getElementById("search")
window.onload = function () {
    let xhr4=new XMLHttpRequest();
    xhr4.open("get","http://localhost:9003/showAllMerchant?flag=1",true)
    xhr4.send()
    xhr4.onload=function () {
        let v= JSON.parse(xhr4.responseText)
        for (var i = 0; i < v.length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td  style=\"border: solid;\">" + v[i].shopName + "</td>" +
                "<td  style=\"border: solid;\">" + "<form action='/toUser' method='get'><input name='userName' value='"+ v[i].userName +"'  hidden><button type='submit'>进店</button></form>" + "</td>" +
                "</tr>"
        }
    }
    let userName = document.getElementById("Uname").value
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:9003/showAllMerchant?flag=2&user_name="+userName,true)
    xhr.send()
    xhr.onload = function () {
        if (JSON.parse(xhr.responseText)[0]!=null){
            document.getElementById("toshop").href="a.html"
        }
    }
    const xhr1 = new XMLHttpRequest();
    xhr1.open("get", "http://localhost:9003/showAllMerchant?flag=3&user_name="+userName,true)
    xhr1.send()
    xhr1.onload = function () {
        if (JSON.parse(xhr1.responseText)[0]!=null){
            document.getElementById("toshop").href="b.html"
        }
    }
}
search.onclick = function () {
    let xhr=new XMLHttpRequest();
    let shopName = document.getElementById("shopName").value
    xhr.open("get", "http://localhost:9003/showAllMerchant?flag=1&shopName=" + shopName, true)
    xhr.send()
    xhr.onload = function () {
        show.innerHTML = ''
        let v = JSON.parse(xhr.responseText)
        for (var i = 0; i < v.length; i++) {
            show.innerHTML += "<tr  style=\"border: solid;\">" +
                "<td  style=\"border: solid;\">" + v[i].shopName + "</td>" +
                "<td  style=\"border: solid;\">" + "<form action='/toUser' method='get'><input name='userName' value='"+ v[i].userName +"'  hidden><button type='button'>进店</button></form>" + "</td>" +
                "</tr>"
        }
        show.innerHTML += "<button onclick='back()'>返回</button>"

    }
}
let add=document.getElementById("add");
add.onclick=function (){
    let searchInput=document.getElementById("searchInput")
    let show=document.getElementById("show")
    show.innerHTML=''
    searchInput.innerHTML=''
    searchInput.innerHTML+="<input id='m' type='number'><button onclick='addM()'>充值</button>"
    searchInput.innerHTML+="<button  onclick='back()'>返回</button>"
}

function addM(){
    let Uname=document.getElementById("Uname").innerText
    let m=document.getElementById("m").value
    let xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:9001/addM?userName="+Uname+"&total="+m,true)
    xhr.send()
    xhr.onload=function (){
        alert(xhr.responseText)
        window.location.href="http://localhost:8888/reload?name="+Uname
    }

}
let logoutBtn = document.getElementById("logoutBtn")
logoutBtn.onclick = function (){
    let Uname=document.getElementById("Uname").innerText
    const xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:8888/logout?name="+Uname+"&kind=USER",true)
    xhr.send()
    window.location.href="http://localhost:8888/back"
}
function back(){
    window.location.href="http://localhost:8888/back"
}
function beComingMerchant(){
    let userName=document.getElementById("userName").value
    let shopName=document.getElementById("shopName").value
    let legal=document.getElementById("legal").value
    let xhr = new XMLHttpRequest();
    xhr.open("get", "http://localhost:9003/beComingMerchant?shopName="+shopName+"&userName="+userName+"&legal="+legal,true)
    xhr.send()
    xhr.onload=function (){
        window.location.href="http://localhost:8888/back"
    }
}
let toshop=document.getElementById("toshop")
toshop.onclick = function (){
    window.location.href="http://localhost:8888/shopSubmit"
}